<%@ page import="com.netcracker.jsp.*" %>
<%@ page import="java.math.BigInteger" %>
<%@ page import="com.netcracker.applications.spfpp.common.core.impl.NCDataObjectManagerImpl" %>
<%@ page import="com.netcracker.applications.opf.pom.ProductOrderValue" %>
<%@ page import="com.netcracker.applications.opf.pom.POMOperations" %>
<%@ page import="com.netcracker.ejb.framework.HomeInterfaceHelper" %>
<%@ page import="com.netcracker.platform.core.ncdo.persistence.PersistenceConfigurationManager" %>
<%@ page import="com.netcracker.platform.core.ncdo.persistence.PersistenceConfiguration" %>
<%@ page import="com.netcracker.applications.opf.pom.inmemory.ProductOrderManager" %>
<%@ page import="com.netcracker.applications.om.opf.OrderManagerConst" %>
<%@ page import="com.netcracker.ejb.attribute.AttributeHome" %>
<%@ page import="com.netcracker.ejb.attribute.*" %>
<%@ page import="com.netcracker.ejb.framework.DateTimeFormatHelper" %>
<%@ page import="com.netcracker.platform.core.ncdo.persistence.PersistenceContext" %>
<%@ page import="com.netcracker.platform.core.ncdo.model.dataobject.MutableNCDataObject" %>
<%@ page import="com.netcracker.ejb.core.NCObject" %>
<%@ page import="com.netcracker.platform.core.ncdo.model.dataobject.impl.MutableNCDataObjectImpl" %>
<%@ page import="com.netcracker.ejb.core.ILoadingService" %>
<%@ page import="com.netcracker.ejb.core.NCObjectHome" %>
<%@ page import="com.netcracker.applications.om.opf.OrderComponentValue" %>
<%@ page import="java.rmi.RemoteException" %>
<%@ page import="javax.ejb.EJBException" %>
<%@ page import="sun.security.util.BigInt" %>
<%@ page import="com.netcracker.applications.decomposition.api14.DecompositionFacade" %>
<%@ page import="com.netcracker.applications.om.opf.manager.actions.DecomposeOrderAction" %>
<%@ page import="java.util.*" %>
<%@ page import="com.netcracker.applications.spf.decomposition.ServiceOrderDecomposer" %>
<%@ page import="com.netcracker.applications.spf.som.ServiceOrderValue" %>
<%@ page import="com.netcracker.applications.spfpp.spf.som.SOMOperations" %>
<%@ page import="com.netcracker.applications.spf.decomposition.DefaultServiceOrderDecomposer" %>
<%@ page import="com.netcracker.applications.om.decomposition.OrderDecomposer" %>
<%@ page import="com.netcracker.applications.decomposition.FullOrderDecomposer" %>
<%@ page import="com.netcracker.applications.spfpp.common.core.PersistenceUtils" %>
<%@ page import="com.netcracker.applications.spfpp.common.core.AbstractPersistenceAction" %>
<%@ page import="com.netcracker.applications.spfpp.spf.som.ServiceOrderManager" %>
<%@ page import="com.netcracker.applications.spfpp.opf.flowcomposer.FlowComposer" %>
<%@ page import="com.netcracker.platform.orchestrator.core.model.*" %>
<%@ page import="com.netcracker.platform.orchestrator.core.model.Process" %>
<%@ page import="com.netcracker.platform.orchestrator.core.orm.SafeLifecycleManager" %>
<%@ page import="com.netcracker.platform.orchestrator.ui.mediation.lifecyclemanagement.ExecutionManager" %>


<%!

    class ThisPage extends ModernPage {
        final String BASE_PERSISTENCE_CONFIG_ALIAS = "pom.persistence.configuration";

        private ProductOrderManager pom;
        private POMOperations pomOperations = null;
        private PersistenceConfigurationManager confManager = null;
        private PersistenceConfiguration config = null;
        private NCDataObjectManagerImpl nCDataObjectManager = new NCDataObjectManagerImpl();
        private BigInteger objectID = new BigInteger("9132562490613416855");
        private BigInteger id = new BigInteger("9132500987513182554");
        private BigInteger sortingKey = null;
        private String dueDate = null;
        private LinkedList localLinkedList = null;

        private String ComID = "9132500987513182580id9132501854213185973";


        protected void parseParameters() {
            //dueDate = getParameter("date");
            //objectID = new BigInteger(getParameter("customerID"));
            //id  = new BigInteger(getParameter("templID"));
            //ComID = getRequest().getParameter("ComponentsID");

        }

        protected void init() throws Exception {

            pom = ProductOrderManager.getInstance();
//            confManager = PersistenceConfigurationManager.getInstance();
//            config = confManager.getPersistenceConfiguration(BASE_PERSISTENCE_CONFIG_ALIAS);
            pomOperations = new POMOperations();
        }

        protected void execute() throws Exception {


            BigInteger attrID = OrderManagerConst.ESTIMATED_DUE_DATE_ATTR_ID;
            AttributeHome attrHome = (AttributeHome) HomeInterfaceHelper.getInstance().lookupHome("com.netcracker.ejb.attribute.AttributeHome", AttributeHome.class);
            Attribute attr = attrHome.findByPrimaryKey(attrID);
            String mask = attr.getMask();
            Date estimatedDueDate = null;
            if (dueDate != null) {
                dueDate = adaptToMask(dueDate, mask);
                estimatedDueDate = DateTimeFormatHelper.getInstance().parseDate(dueDate, mask);
                sortingKey = new BigInteger(Long.toString(estimatedDueDate.getTime()));
            }

            PersistenceContext pc = null;
            final BigInteger customerID = objectID;

            try {
                final ProductOrderManager productOrderManager = pom;
//                            pc = nCDataObjectManager.createContext(config);
                new PersistenceUtils().doInPersistenceContext(
                        new AbstractPersistenceAction() {
                            public Object run() {
                                BigInteger piID = null;
                                ProductOrderValue pov;
                                pov = pomOperations.createNewProductOrderValue(id, customerID, sortingKey);

                                if (pov != null) {
                                    BigInteger newOrderID = null;
                                        final MutableNCDataObject newOrder = productOrderManager.createOrder(pov);
                                        newOrderID = newOrder.getId();

										BigInteger componentTemplateId = new BigInteger("9132500987513182554");
                                        OrderComponentValue ocv = pomOperations.createProductOrderComponentValue(componentTemplateId, null, OrderManagerConst.ORDER_COMPONENT_STATUS_CREATED_VALUE);
										
										MutableNCDataObject newComponent = pom.addComponent(newOrderID, ocv);
										pom.startOrder(newOrderID);

										MutableNCDataObject parentObj = nCDataObjectManager.findObjectByID(OrchestrationConstants.INSTANCE_PROCESSES_TOPSITE_ID);
										FlowComposer flowComposer = new FlowComposer();

										Process process = flowComposer.createExecutionFlow(parentObj, (List<MutableNCDataObject>) orders);
										SafeLifecycleManager.getInstance().storeProcess(process);
                                            ExecutionManager.process(process.getID(), "execute_process");

                                        return null;
                                }
                                return null;
                            }
                        }, BASE_PERSISTENCE_CONFIG_ALIAS);

            } catch (Exception e) {

                new RuntimeException(e);
            } finally {
                if (pc != null) {
                    pc.close();
                }
            }
        }

        protected String adaptToMask(String date, String mask) {
            char[] separator = {'/', ':', '_', ' '};
            char[] dateCh = date.toCharArray();
            char[] maskCh = mask.toCharArray();
            int endPos;
            if (mask.length() >= dateCh.length)
                endPos = dateCh.length;
            else endPos = mask.length();
            for (int i = 0; i < endPos; i++) {
                for (int j = 0; j < separator.length; j++)
                    if (dateCh[i] == separator[j] && dateCh[i] != maskCh[i])
                        dateCh[i] = maskCh[i];
            }
            date = String.valueOf(dateCh);
            return date;
        }

        @Override
        public void printWindowContent() throws Exception {

        }
    }

%>

<%
    ThisPage thePage = new ThisPage();
    thePage.setPageContext(pageContext);
    try {
        thePage.debugSecureService();
    } finally {
    }
%>
